package com.iqiyi.pps.epg.web.timer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.core.model.channel.ChannelConfig;
import com.iqiyi.pps.epg.core.model.partition.Partition;
import com.iqiyi.pps.epg.core.service.channel.ChannelService;
import com.iqiyi.pps.epg.core.service.partitions.PartitionsService;
import com.iqiyi.pps.epg.core.utils.ApiAlertPlatFormUtil;
import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import com.iqiyi.pps.epg.core.utils.WriteSystemLogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

public class PartitionsWarnningTrigger {
    private static Logger logger = LoggerFactory.getLogger(PartitionsWarnningTrigger.class);
    @Autowired
    private PartitionsService partitionsService;

    private ChannelService getChannelService() {
        return WriteSystemLogUtil.getInstance().getChannelService();
    }

    public void task() {
        logger.debug("[PartitionsWarnningTrigger][task] start");
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DATE, 1);
        StringBuffer partitionName = new StringBuffer();
        partitionName.append("p").append(DateTimeUtil.transStrFormat(now.getTimeInMillis()));
        List<String> tableNames = partitionsService.getTableNames();
        List<Partition> partitions = partitionsService.getPartition(partitionName.toString());
        Iterator<String> tableNameIt = tableNames.iterator();
        while(tableNameIt.hasNext()) {
            boolean flag = false;
            String tableName = tableNameIt.next();
            for (Partition partition : partitions) {
                if (tableName.equals(partition.getTableName())) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                tableNameIt.remove();
            }
        }

        if (tableNames.size() > 0) {
            JSONObject jsonObject = new JSONObject();
            StringBuffer warnTableNames = new StringBuffer();
            for (String str : tableNames) {
                warnTableNames.append(str).append(";");
            }
            jsonObject.put("content", "[警告][EPG] 数据表：" + warnTableNames.toString() + "分区名：" + partitionName.toString() + "没有创建");
            List<ChannelConfig> channelConfigs = getChannelService().getConfig(ApiAlertPlatFormUtil.PARTITION_WARNNING_TYPE);
            StringBuffer users = new StringBuffer();
            for (ChannelConfig entity : channelConfigs) {
                users.append(entity.getVal()).append(",");
            }
            if (users.length() > 0) {
                ApiAlertPlatFormUtil.doSend(ApiAlertPlatFormUtil.TOPIC_ID_LIVE_RECORD, ApiAlertPlatFormUtil.SECRET_KEY_EPG, jsonObject.toJSONString(), users.toString(), ApiAlertPlatFormUtil.SEND_TYPE_CHAT);
            }
        }


    }

}
